/**
 * 链式调用和延迟执行
 */

function arrange(name) {
  const tasks = [];

  tasks.push(() => console.log(`${name} is notified`));

  function wait(time) {
    tasks.push(() => new Promise(resolve => setTimeout(resolve, time * 1000)));
    return this;
  }

  function doSomething() {
    tasks.push(() => console.log("Start to commit"));
    return this;
  }

  function waitFirst(time) {
    tasks.unshift(() => new Promise(resolve => setTimeout(resolve, time * 1000)));
    return this;
  }

  async function execute() {
    for (const task of tasks) {
      await task();
    }
    return this;
  }

  return {
    wait,
    do: doSomething,
    waitFirst,
    execute
  };
}

arrange("Xiaoxu").wait(5).do("commit").waitFirst(3).execute();

/**
 * 等待3秒
 * 输出Xiaoxu is notified
 * 等待5秒
 * 输出Start to Commit
 */
